﻿using AutoMapper;
using BPMN.Application.Contracts.BaseWay;
using BPMN.Application.Contracts.DTO.Defective;
using BPMN.Application.Contracts.DTO.ProceSS;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.IService;
using BPMN.Domain.Entity.DefectiveModel;
using BPMN.Domain.Interface;
using BPMN.Domain.ProcessModel;
using BPMN.Infrastructure.Repository;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPMN.Application.Service
{
    public class ProcesService : IProcesService
    {
        public ProcesService(IRepositoryBase<ProcessShows> processShow, IRepositoryBase<Reporting> reporting, IRepositoryBase<Defective> defective)
        {
            ProcessShow = processShow;//工序表

            Reporting = reporting;//报工表

            Defective = defective;//不良品相表
        }

        public IRepositoryBase<ProcessShows> ProcessShow { get; }

        public IRepositoryBase<Reporting> Reporting { get; }
        public IRepositoryBase<Defective> Defective { get; }



        /// <summary>
        /// 工序显示数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<ProCcessShowDto>>> ProCcessShow(string Process_BianHao, string Process_Name, int Reporting_Id, int Defective_Id, decimal PeiBiMin, decimal PeibiMax, string dateMax, string dateMin)
        {
            //获取报工表
            var ReporTing = Reporting.GetAll().Result.ToList();
            //获取工序表
            var ProCcess = ProcessShow.GetAll().Result.ToList();
            //获取不良产品表
            var DefectiveShow = Defective.GetAll().Result.ToList();


            var liq = from a in ReporTing
                      join b in ProCcess on a.Reporting_Id equals b.Reporting_Id
                      join c in DefectiveShow on b.Defective_Id equals c.Defective_Id
                      select new ProCcessShowDto
                      {
                          Process_Id = b.Process_Id,
                          Process_BianHao = b.Process_BianHao,
                          Process_Name = b.Process_Name,
                          Defective_Id = b.Defective_Id,
                          Process_DuiBi = b.Process_DuiBi,
                          Process_KuoZhanZIdUAN = b.Process_KuoZhanZIdUAN,
                          Reporting_Id = b.Reporting_Id,
                          CreatorId = b.CreatorId,
                          CreateDate = b.CreateDate,
                          UpdateDate = b.UpdateDate,
                          Updator = b.Updator,
                          //逻辑删除字段
                          IsDel = b.IsDel,
                          //报工的name
                          Reporting_Name = a.Reporting_Name,

                          //不良品相名称
                          Defective_Name = c.Defective_Name
                      };

            if (!string.IsNullOrEmpty(Process_BianHao))
            {
                liq = liq.Where(x => x
                .Process_BianHao.Contains(Process_BianHao));
            }
            if (!string.IsNullOrEmpty(Process_Name))
            {
                liq = liq.Where(x => x
                .Process_Name.Contains(Process_Name));
            }
            if (Reporting_Id != 0)
            {
                liq = liq.Where(x => x
               .Reporting_Id == Reporting_Id);

            }
            if (Defective_Id != 0)
            {
                liq = liq.Where(x => x
               .Defective_Id == Defective_Id);

            }
            if (PeiBiMin != 0)
            {
                liq = liq.Where(x => x.Process_DuiBi >= PeiBiMin);


            }
            if (PeibiMax != 0)
            {
                liq = liq.Where(x => x.Process_DuiBi <= PeibiMax);


            }

            if (!string.IsNullOrEmpty(dateMax))
            {

                liq = liq.Where(x => x.CreateDate >= Convert.ToDateTime(dateMax));

            }
            if (!string.IsNullOrEmpty(dateMin))
            {

                liq = liq.Where(x => x.CreateDate <= Convert.ToDateTime(dateMin));

            }









            return new ResultResponseDto<List<ProCcessShowDto>>()
            {
                ErrorInfo = "获取成功",
                Code = DomianShare.ResultCode.Ok,
                Data = liq.ToList()
            };
        }



        /// <summary>
        /// 工序单表显示数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<ProcessShows>>> GetResultShows()
        {
            var info = (await ProcessShow.GetAll()).ToList();

            return new ResultResponseDto<List<ProcessShows>>()
            {
                Code = DomianShare.ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = info.ToList()
            };
        }




        /// <summary>
        /// 报工权限
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<Reporting>>> GetResult()
        {
            var info = (await Reporting.GetAll()).ToList();

            return new ResultResponseDto<List<Reporting>>()
            {
                Code = DomianShare.ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = info.ToList()
            };
        }


        /// <summary>
        /// 不良品相显示数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<Defective>>> GetDefective()
        {
            var info = (await Defective.GetAll()).ToList();

            return new ResultResponseDto<List<Defective>>()
            {
                Code = DomianShare.ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = info.ToList()
            };
        }







        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public async Task<int> ResultAdd(ProcessShows process)
        {
            process.CreateDate = DateTime.Now;
            process.Updator = 1;
            process.CreatorId = 1;
            process.UpdateDate = DateTime.Now;
            return await ProcessShow.Add(process);
        }







    }












}

